﻿using LPLS.Commons.Mappers;
using LPLS.DBEntities.ConfigModels.AlarmModels;
using LPLS.DBEntities.ConfigModels.AlarmModels.AlarmVarableCodeRuleConfigModels;
using LPLS.DBInterface.ConfigModels.AlarmModel.AlarmVarableCodeRuleConfigModels;
using Microsoft.Win32;
using MiniExcelLibs;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace PLCAlarmRuleConfigModule.ViewModels
{
    /// <summary>
    /// 报警代码规则配置
    /// </summary>
    public  class AlarmVarableCodeRuleConfigViewModel : BindableBase
    {
        private readonly IAlarmVarableCodeRuleConfigService _alarmVarableConfigService;


        public AlarmVarableCodeRuleConfigViewModel(IAlarmVarableCodeRuleConfigService  alarmVarableCodeRuleConfigService    ) 
        { 


            OpenFileCommand = new DelegateCommand(OpenFileDialog);
            AlarmVarCodeRuleList=new ObservableCollection<AlarmVarableCodeRuleConfig>();

            _alarmVarableConfigService = alarmVarableCodeRuleConfigService;
            LoadData();

        }

  
        public DelegateCommand OpenFileCommand { get; private set; }

   

        private void OpenFileDialog()
        {
            Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog
            {
                Title = "选择Excel文件",
                Filter = "Excel Files|*.xlsx;*.xls|All Files|*.*"
            };

            if (openFileDialog.ShowDialog() == true)
            {
                SelectedFilePath = openFileDialog.FileName;
                LoadExcelData(openFileDialog.FileName);
                MessageBox.Show($"选择的文件路径: {SelectedFilePath}");
            }
        }


        private string _selectedFilePath;
        public string SelectedFilePath
        {
            get => _selectedFilePath;
            set => SetProperty(ref _selectedFilePath, value);
        }


        private List<AlarmVarCodeRuleExcelModel> excelModels = new List<AlarmVarCodeRuleExcelModel>();

        private void LoadExcelData(string path)

        {

            excelModels.Clear();
            var sheets = MiniExcel.GetSheetNames(path);
            foreach (var sheet in sheets)
            {
                Console.WriteLine($"sheet name : {sheet} ");
                var rows = MiniExcel.Query<AlarmVarCodeRuleExcelModel>(path, sheetName: sheet);
                excelModels.AddRange(rows);///获取到了excel数据 现在需要对数据进行入库操作
                Console.WriteLine(rows);
            }

            ///获取到了Excel数据了List<AlarmVarableConfig>
            var dbdata = ReflectionMapper.MapList<AlarmVarCodeRuleExcelModel, AlarmVarableCodeRuleConfig>(excelModels);
            if (dbdata.Count==0)
            {
                 MessageBox.Show("导入的数据表不正确");
                return;
            }
            _alarmVarableConfigService.SaveData(dbdata);
            LoadData();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {

            AlarmVarCodeRuleList.Clear();

            var mydbdata = _alarmVarableConfigService.GetData();
            AlarmVarCodeRuleList = new ObservableCollection<AlarmVarableCodeRuleConfig>(mydbdata);

        }



        private ObservableCollection<AlarmVarableCodeRuleConfig> _AlarmVarCodeRuleList;

        public ObservableCollection<AlarmVarableCodeRuleConfig> AlarmVarCodeRuleList
        {
            get { return _AlarmVarCodeRuleList; }
            set { 
                SetProperty(ref _AlarmVarCodeRuleList, value);

               }
        }

    }
}
